/**
 *  Copyright(C) 2012-2013 Suntec(Shanghai) Software Co., Ltd.
 *  All Right Reserved.
 */
package com.cugbmao.lucene;

import java.io.IOException;

import junit.framework.TestCase;

import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 * Descriptions
 * 
 * @version 2013年5月31日
 * @author Suntec
 * @since JDK1.6
 * 
 */
public class PhraseQueryTest extends TestCase {
	private Directory dir;
	private IndexReader reader;

	/*
	 * (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws IOException {
		dir = new RAMDirectory();
		IndexWriterConfig iwf = new IndexWriterConfig( Version.LUCENE_43, new WhitespaceAnalyzer( Version.LUCENE_43 ) );
		IndexWriter writer = new IndexWriter( dir, iwf );
		Document doc = new Document();
		doc.add( new TextField( "field", "the quick brown fox jumped over the lazy dog", Field.Store.YES ) );
		writer.addDocument( doc );
		writer.close();

		reader = DirectoryReader.open( dir );
	}

	/*
	 * (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws IOException {
		reader.close();
		dir.close();
	}

	private boolean matched( String[] phrase, int slop ) throws IOException {
		PhraseQuery query = new PhraseQuery();
		query.setSlop( slop );

		for ( String word : phrase ) {
			query.add( new Term( "field", word ) );
		}

		TopDocs docs = new IndexSearcher( reader ).search( query, 10 );
		return docs.totalHits > 0;
	}

	public void testSlopComparison() throws Exception {
		String[] phrase = new String[] { "quick", "fox" };
		assertFalse( "exact phrase not found", matched( phrase, 0 ) );
		assertTrue( "close enough", matched( phrase, 1 ) );
	}

	public void testReverase() throws Exception {
		String[] phrase = new String[] { "fox", "quick" };
		assertFalse( "hop flop", matched( phrase, 2 ) );
		assertTrue( "hop hop flop", matched( phrase, 3 ) );
	}

	public void testMulitple() throws Exception {
		assertFalse( "not close enough", matched( new String[] { "quick", "jumped", "lazy" }, 3 ) );
		assertTrue( "just enough", matched( new String[] { "quick", "jumped", "lazy" }, 4 ) );
		assertFalse( "almost but not quite", matched( new String[] { "lazy", "jumped", "quick" }, 7 ) );
		assertTrue( "bingo", matched( new String[] { "lazy", "jumped", "quick" }, 8 ) );
	}

}
